package com.workmanager.controller;

import com.workmanager.bean.*;
import com.workmanager.mapper.HomePageMapper;
import com.workmanager.service.HomePageService;
import com.workmanager.service.StudentWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by YIJIE on 2016/9/2.
 */
@RequestMapping("/StudentWorkController")
@Controller
public class StudentWorkController {
    @Autowired
    @Resource(name = "studentWorkService")
    private StudentWorkService studentWorkService;
    @Resource(name="homePageService")
    private HomePageService homePageService;
    //获得我的历史作业
    @RequestMapping("/getMywork")
    public String getMywork(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession(false);
        StudentBean studentBean = (StudentBean) session.getAttribute("学生");
        int student_id= Integer.parseInt(studentBean.getStudent_id());
//        int student_id=1
        if(studentBean.getClass_id()!=null){
            List<WorkBean> list = studentWorkService.getAllworkById(student_id);
            String myworknum = studentWorkService.getMyTaskNum(student_id);
            String jobtasknum = studentWorkService.getJobTaskNum(Integer.parseInt(studentBean.getClass_id()));
            request.setAttribute("allmywork",list);
            request.setAttribute("myworknum",myworknum);
            request.setAttribute("jobtasknum",jobtasknum);
            return "/student/checkHistorywork.jsp";
        }
        else {
            return "/student/checkHistorywork.jsp";
        }
    }
    //获得老师评价
    @RequestMapping("/getEvaluate")
    public String getEvaluate(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int work_id=Integer.parseInt(request.getParameter("work_id"));
        WorkBean workBean = studentWorkService.getWorkById(work_id);
        List<EvaluateBean> evaluateBean = studentWorkService.getEvaluateById(work_id);
        List<FeedbackBean> feedbacks = studentWorkService.getFeedbackById(work_id);
        request.setAttribute("feedbacks",feedbacks);
        request.setAttribute("awork",workBean);
        request.setAttribute("aevaluate",evaluateBean);
        return "/student/feedback.jsp";
    }
    //主页获取班级
    @RequestMapping("/homePage")
    public String homePage(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String flag =  request.getParameter("flag");
        if("stu".equals(flag)){
            StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
            List<JobTaskBean> listJobTask = homePageService.getUnfinishedWork(student.getStudent_id());
            request.setAttribute("jobtask",listJobTask);
            return "/student/homePage.jsp";
        }else if("tea".equals(flag)) {
            TeacherBean tea = (TeacherBean) request.getSession().getAttribute("教师");
            List<WorkBean> listWork = homePageService.getFinishedWork(tea.getTeacher_id());
            request.setAttribute("work",listWork);
            return "/teacher/homePage.jsp";
        }else {
            return "msg.jsp";
        }

    }

    @RequestMapping("/giveFeedback")
    public String giveFeedback(HttpServletRequest request, HttpServletResponse response) throws IOException{
        FeedbackBean feedbackBean = new FeedbackBean();
        feedbackBean.setWork_id(request.getParameter("work_id"));
        feedbackBean.setFeedback_content(request.getParameter("feedInfo"));
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
        feedbackBean.setFeedback_time(date);
        boolean result= false;
        result = studentWorkService.insertfeed(feedbackBean);
        String url="/msg.jsp";
        if(result){
            request.setAttribute("msgtitle","反馈成功");
            request.setAttribute("msgcontent","恭喜你反馈成功");
            request.setAttribute("msgurl","/StudentWorkController/getMywork.wm");
        }
        else{
            request.setAttribute("msgtitle","反馈失败");
            request.setAttribute("msgcontent","请重新进行反馈");
            request.setAttribute("msgurl","/StudentWorkController/getMywork.wm");
        }
        return url;
    }

    //进行留言
    @RequestMapping("/giveComments")
    public String giveComments(HttpServletRequest request, HttpServletResponse response) throws IOException{
        HttpSession session=request.getSession(false);
        StudentBean studentBean = (StudentBean) session.getAttribute("学生");
        int student_id=Integer.parseInt(studentBean.getStudent_id());
        TeacherBean teacherBean= studentWorkService.getTeacherById(student_id);
        List<LeaveMessageBean> leaveMessages=studentWorkService.getmessagebystudentid(student_id);
        request.setAttribute("ateacher",teacherBean);
        request.setAttribute("student_id",student_id);
        request.setAttribute("leaveMessages",leaveMessages);
        return "/student/comments.jsp";
    }

    //提交留言
    @RequestMapping("/submitComments")
    public String submitComments(HttpServletRequest request, HttpServletResponse response) throws IOException{
        LeaveMessageBean leaveMessageBean = new LeaveMessageBean();
        leaveMessageBean.setTeacher_id(request.getParameter("teacher_id"));
        leaveMessageBean.setStudent_id(request.getParameter("student_id"));
        leaveMessageBean.setLeaveMessage_content(request.getParameter("feedInfo"));
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
        leaveMessageBean.setLeaveMessage_date(date);;
        leaveMessageBean.setLeaveMessage_from("student");
        boolean result= false;
        result = studentWorkService.submitComments(leaveMessageBean);
        String url="/msg.jsp";
        if(result){
            request.setAttribute("msgtitle","留言成功");
            request.setAttribute("msgcontent","恭喜你留言成功");
            request.setAttribute("msgurl","/student/homePage.jsp");
        }
        else{
            request.setAttribute("msgtitle","留言失败");
            request.setAttribute("msgcontent","请重新进行留言");
            request.setAttribute("msgurl","/student/homePage.jsp");
        }
        return url;
    }

    public StudentWorkService getStudentWorkService() {
        return studentWorkService;
    }

    public void setStudentWorkService(StudentWorkService studentWorkService) {
        this.studentWorkService = studentWorkService;
    }

    public HomePageService getHomePageService() {
        return homePageService;
    }

    public void setHomePageService(HomePageService homePageService) {
        this.homePageService = homePageService;
    }
}
